# -*- coding: utf-8 -*-
# created on 2016/6/3
from mathsolver.functions.root.jiefangchen import JieFangChen
from mathsolver.functions.base import *
from sympy import solveset, S, reduce_inequalities, Rel
from mathsolver.functions.sympy_utils import simplify_ineq_solveset, safe_degree
from mathsolver.functions.fangcheng.base import eryuanyicifangchengzu_judge, fenshi_judge, yicifangcheng_judge
import itertools


class EqEqTof(BaseFunction):
    def solver(self, *args):
        f1, f2 = args[0].sympify()
        f3, f4 = args[1].sympify()

        eq1 = (f1 - f2).simplify()
        eq2 = (f3 - f4).simplify()

        if str(eq1) == str(eq2):
            answer = True
        else:
            ans1 = solveset(f1 - f2, None, S.Reals)
            ans2 = solveset(f3 - f4, None, S.Reals)

            if ans1 == ans2:
                answer = True
            else:
                answer = False

        if answer:
            self.steps.append(["", "%s与%s等价" % (self.output_eq([f1, f2]), self.output_eq([f3, f4]))])
        else:
            self.steps.append(["", "%s与%s不等价" % (self.output_eq([f1, f2]), self.output_eq([f3, f4]))])

        self.label.add("判断方程是否正确")
        self.output.append(BaseVariable(answer))
        return self


class IneqIneqTof(BaseFunction):
    def solver(self, *args):
        f1, op1, f2 = args[0].sympify()
        f3, op2, f4 = args[1].sympify()
        symbol = (f1 - f2).free_symbols.pop()

        new_ineq1 = reduce_inequalities(Rel(f1, f2, op1), symbol)
        new_ineq2 = reduce_inequalities(Rel(f3, f4, op2), symbol)

        new_ineq1 = simplify_ineq_solveset(new_ineq1)
        new_ineq2 = simplify_ineq_solveset(new_ineq2)

        if (new_ineq2.is_Boolean and new_ineq2) or str(new_ineq1) == str(new_ineq2):
            self.steps.append(["", "%s与%s等价" % (new_latex(new_ineq1), new_latex(new_ineq2))])
            self.output.append(BaseVariable(True))
        else:
            self.steps.append(["", "%s与%s不等价" % (new_latex(new_ineq1), new_latex(new_ineq2))])
            self.output.append(BaseVariable(False))
        self.label.add("判断不等式是否正确")
        return self


class FangChenTofYiYuanYiCi(BaseFunction):
    symbol_num = 1
    degree_num = 1
    name = "一元一次方程"

    def solver(self, *args):
        f1, f2 = args[0].sympify()
        eq = (f1 - f2).expand().simplify()
        answer = True
        symbols = eq.free_symbols

        if len(symbols) != self.symbol_num:
            answer = False
        else:
            if self.symbol_num > 1:
                for a1, a2 in itertools.combinations(symbols, 2):
                    if eq.has(a1 * a2):
                        answer = False

            for symbol in symbols:
                if safe_degree(eq, symbol) != self.degree_num:
                    answer = False
                    break

        if answer:
            self.steps.append(["", "%s是%s" % (self.output_eq([f1, f2]), self.name)])
        else:
            self.steps.append(["", "%s不是%s" % (self.output_eq([f1, f2]), self.name)])

        self.label.add("%s的定义" % self.name)
        self.output.append(BaseVariable(answer))
        return self


class FangChenTofYiYuanErCi(FangChenTofYiYuanYiCi):
    degree_num = 2
    name = "一元二次方程"


class FangChenTofErYuanYiCi(FangChenTofYiYuanYiCi):
    degree_num = 1
    symbol_num = 2
    name = "二元一次方程"


class FangChenTofErYuanYiCiZu(BaseFunction):
    def solver(self, *args):
        eqs = args[0].sympify()
        answer = eryuanyicifangchengzu_judge(eqs)

        if answer:
            self.steps.append(["", "%s是二元一次方程组" % (BaseEqs(eqs).printing())])
        else:
            self.steps.append(["", "%s不是二元一次方程组" % (BaseEqs(eqs).printing())])

        self.label.add("判断是否是二元一次方程组")
        self.output.append(BaseVariable(answer))
        return self


class FangChenTofFenShi(BaseFunction):
    def solver(self, *args):
        eq = args[0].sympify()
        answer = False
        if isinstance(args[0], BaseEq):
            symbol = default_symbol(eq[0] - eq[1])
            answer = fenshi_judge(eq, symbol)

        if answer:
            self.steps.append(["", "%s是分式方程" % (BaseEq(eq).printing())])
        else:
            self.steps.append(["", "%s不是分式方程" % (BaseEq(eq).printing())])

        self.label.add("判断是否为分式方程")
        self.output.append(BaseVariable(answer))
        return self


class FangChenTofFangCheng(BaseFunction):
    def solver(self, *args):
        answer = False
        eq = args[0].sympify()
        if isinstance(args[0], BaseEq):
            fangcheng = eq[0] - eq[1]
            symbols = fangcheng.free_symbols
            if len(symbols) > 0:
                answer = True

        if answer:
            self.steps.append(["", "%s是方程" % (BaseEq(eq).printing())])
        else:
            self.steps.append(["", "%s不是方程" % (BaseEq(eq).printing())])

        self.label.add("判断是否为方程")
        self.output.append(BaseVariable(answer))
        return self


class FangChenTofYiYuanYiCiBuDengShi(BaseFunction):
    def solver(self, *args):
        ineq = args[0].sympify()
        answer = False

        if isinstance(args[0], BaseIneq):
            symbols = (ineq[0] - ineq[2]).free_symbols
            if len(symbols) == 1:
                symbol = default_symbol(ineq[0] - ineq[2])
                answer = yicifangcheng_judge(ineq[0] - ineq[2], symbol)

        if answer:
            self.steps.append(["", "%s是一元一次不等式" % (BaseIneq(ineq).printing())])
        else:
            self.steps.append(["", "%s不是一元一次不等式" % (BaseIneq(ineq).printing())])

        self.label.add("判断是否为一元一次不等式")
        self.output.append(BaseVariable(answer))
        return self


class FangChenTofYiYuanYiCiBuDengShiZu(BaseFunction):
    def solver(self, *args):
        step_ineqs = args[0].sympify()
        answer = False
        if isinstance(args[0], BaseIneqs):
            symbols = args[0].free_symbols()
            if len(symbols) == 1:
                symbol = list(symbols)[0]
                for ineq in step_ineqs:
                    answer = yicifangcheng_judge(ineq[0] - ineq[2], symbol)

        if answer:
            self.steps.append(["", "%s是一元一次不等式组" % (BaseIneqs(step_ineqs).printing())])
        else:
            self.steps.append(["", "%s不是一元一次不等式组" % (BaseIneqs(step_ineqs).printing())])

        self.label.add("判断是否为一元一次不等式组")
        self.output.append(BaseVariable(answer))
        return self


# 判断方程的有没有实数根
class FangChenTofErCi001(BaseFunction):
    def solver(self, *args):
        answer = False
        eq = args[0].sympify()
        jies = JieFangChen().solver(args[0]).output[0].value
        if jies[jies.keys()[0]]:
            answer = True
        if answer:
            self.steps.append(["", "%s有实数根" % (BaseEq(eq).printing())])
        else:
            self.steps.append(["", "%s没有实数根" % (BaseEq(eq).printing())])
        self.output.append(BaseVariable(answer))
        return self


if __name__ == '__main__':
    pass
